package com.baoxin.chenxixc4coach;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.baoxin.chenxixc4coach.bean.Potential;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.NetUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class GenjinDetailActivity extends BaseActivity {

    Potential p;
    private TextView mName;
    private TextView mTime;
    private EditText mRemark;
    private Button mBt1;//继续跟进
    private Button mBt2;//确定报名
    private Button mBt3;//结束跟进


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_genjin_detail);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);//去掉默认title
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mName = findViewById(R.id.name);
        mTime = findViewById(R.id.time);
        mRemark = findViewById(R.id.remark);
        mBt1 = findViewById(R.id.jxgj);
        mBt2 = findViewById(R.id.qdbm);
        mBt3 = findViewById(R.id.jsgj);
        mBt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeState(1);
            }
        });
        mBt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeState(2);
            }
        });
        mBt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeState(3);
            }
        });

        p = getIntent().getParcelableExtra("potential");

        mName.setText(p.getCtConTrname());
        mTime.setText("咨询时间：" + p.getCtConTime());
        mRemark.setText(p.getCtConRemark());

    }

    public void onCall(View view) {
        Intent intent = new Intent(Intent.ACTION_DIAL);//不是CALL不需要运行时权限
        Uri data = Uri.parse("tel:" + p.getCtConTrphone());
        intent.setData(data);
        startActivity(intent);
    }

    private void changeState(int state) {
        JSONObject body = new JSONObject();
        try {
            body.put("ctConId", p.getCtConId());
            body.put("ctConRemark", mRemark.getText().toString());
            body.put("ctConState", state);

            NetUtil.post(this, Interfaces.URL_GENJIN_CHANGE, body.toString(), new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String result = response.body().string();

                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        String code = jsonObject.getString("code");

                        if (code.equals("0")) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(GenjinDetailActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                                }
                            });

                            setResult(0);
                            finish();
                        } else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(GenjinDetailActivity.this, "修改失败！", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
